package com.voltmao.webpro.utils.wechat.utils;

import com.voltmao.webpro.utils.JsonUtil;
import com.voltmao.webpro.utils.http.HttpClientUtil;
import com.voltmao.webpro.utils.wechat.IWechatUtils;
import com.voltmao.webpro.utils.wechat.core.AccessToken;

import java.util.LinkedList;
import java.util.List;
import java.util.Map;

/**
 * Created by zhouyang on 2017/1/6.
 */
public class MenuUtils {

    private List<Object> rootMenus = new LinkedList<>();

    private Map<String, String> wxConfig;
    private AccessToken accessToken;

    public MenuUtils(IWechatUtils wechatUtils) {
        this.wxConfig = wechatUtils.getWxConfig();
        this.accessToken = wechatUtils.getAccessToken();
    }

    /**
     * 菜单接口
     */
    public interface MenuInterface {
        void addRootMenu(List<Object> rootMenus);
    }

    /**
     * 创建微信菜单
     *
     * @param menuInterface
     */
    public void createMenu(MenuInterface menuInterface) {
        menuInterface.addRootMenu(rootMenus);

        String sendUrl = wxConfig.get("wx.menu.create")
                .replace("ACCESS_TOKEN", this.accessToken.getAccessToken());
        String postTxt = "{\"button\":" + JsonUtil.ObjectToJSON(rootMenus) + "}";

        HttpClientUtil.sendPostToString(sendUrl, postTxt);
    }

}